<template>
  <a-modal
    :visible="visible"
    title="添加人员"
    @cancel="handleClose"
    @ok="ok"
    :width="600"
  >
    <div class="about-master-container">
      <a-form-model>
        <a-form-model-item prop="rectificationPeople1" label="班组">
          <BSelect ref="bSelect" style="width: 300px;" :request="apiGroup" v-model="form.groupId" />
        </a-form-model-item>
        <a-form-model-item prop="rectificationPeople1" label="人员">
          <BSelect :isClear="true" :init="false" :form="form" keyValue="groupId" style="width: 300px;"
                   :request="getSubTeamPeople"
                   labelInValue
                   v-model="form.people"
          />
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>


import { getSubTeam } from '@/api/project/projects'
import { men } from '@/api/quality/quality'

export default {
  props: {
    subContractorId: {
      type: String,
      default: ''
    },
    projectId: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      form: {},
      apiGroup: async () => {
        return men.getTeamList({
          subContractorId: this.subContractorId,
          projectId: this.projectId
        })
      },
      getSubTeamPeople: async () => {
        let res = await getSubTeam({ id: this.form.groupId })
        if (res.code === this.SUCCESS_CODE) {
          return { code: this.SUCCESS_CODE, data: res.data.teamMembers }
        }
        return { code: this.SUCCESS_CODE, data: [] }
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
    },
    ok() {
      this.$console(this.form)
      //
      if (!this.form.people) {
        return this.$noticeWarning('请选择人员')
      }
      this.$emit('ok', this.form)
      this.handleClose()
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.$nextTick(() => {
          this.$refs.bSelect.getData()
        })
      } else {
        // this.form = { groupId: '', people: {} }
        this.form = {}
      }
    }
  }
}
</script>

